﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 处理2011、2051、2061、2031指令
    /// </summary>
    public static class UplineDataInstructionSpecialMonitor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineDataInstructionSpecialMonitor));

        /// <summary>
        /// 处理2011指令
        /// </summary>
        /// <param name="content"></param>
        public static void Process2011Hf(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport1 = CommonInformation.GetLetportByMn(mn);
                    if (letport1 == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                    if (enterprise == null) return;
                    if (!enterprise.企业名称.Contains("恒发")) return;
                    var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                        .FirstOrDefault(t => t.ID != letport1.ID);
                    if (letport2 == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute / 10 * 10, 0);
                    var end = begin.AddMinutes(10);
                    RealDataMain master1 = pCtx.RealDataMains.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport1.ID)
                        .OrderByDescending(t => t.监测时间)
                        .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    if (master1 == null) return;
                    RealDataMain master2 = pCtx.RealDataMains.Where(t => t.企业基本信息ID == letport2.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport2.ID)
                        .OrderByDescending(t => t.监测时间)
                        .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    if (master2 == null) return;
                    List<RealDataDetail> allDetails = new List<RealDataDetail>();
                    var details1 = master1.RealDataDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.RealDataDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }

                    bool flag1 = false;
                    bool flag2 = false;
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            flag1 = true;
                            detail1 = new RealDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                RTD = item.RTD
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            flag2 = true;
                            detail2 = new RealDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                RTD = item.RTD
                            };
                            details2.Add(detail2);
                        }
                    }
                    if (flag1)
                    {
                        master1.是否下发 = false;
                    }
                    if (flag2)
                    {
                        master2.是否下发 = false;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2051Hf(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport1 = CommonInformation.GetLetportByMn(mn);
                    if (letport1 == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                    if (enterprise == null) return;
                    if (!enterprise.企业名称.Contains("恒发")) return;
                    var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                       .FirstOrDefault(t => t.ID != letport1.ID);
                    if (letport2 == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute / 10 * 10, 0);
                    var end = begin.AddMinutes(10);
                    var master1 = pCtx.MinuteDataMains.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport1.ID)
                        .OrderByDescending(t => t.监测时间)
                        .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    if (master1 == null) return;
                    var master2 = pCtx.MinuteDataMains.Where(t => t.企业基本信息ID == letport2.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport2.ID)
                        .OrderByDescending(t => t.监测时间)
                        .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    if (master2 == null) return;
                    List<MinuteDataDetail> allDetails = new List<MinuteDataDetail>();
                    var details1 = master1.MinuteDataDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.MinuteDataDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    bool flag1 = false;
                    bool flag2 = false;
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            flag1 = true;
                            detail1 = new MinuteDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master1.时段量 * item.Avg / 1000.0//item.Cou
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            flag2 = true;
                            detail2 = new MinuteDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master2.时段量 * item.Avg / 1000.0//item.Cou
                            };
                            details2.Add(detail2);
                        }
                    }
                    if (flag1)
                    {
                        master1.是否下发 = false;
                    }
                    if (flag2)
                    {
                        master2.是否下发 = false;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2061Hf(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport1 = CommonInformation.GetLetportByMn(mn);
                    if (letport1 == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                    if (enterprise == null) return;
                    if (!enterprise.企业名称.Contains("恒发")) return;
                    var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                     .FirstOrDefault(t => t.ID != letport1.ID);
                    if (letport2 == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, 0, 0);
                    var end = begin.AddHours(1);

                    var master1 = pCtx.HourDataMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport1.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);
                    if (master1 == null) return;
                    var master2 = pCtx.HourDataMains.Where(t => t.企业基本信息ID == letport2.企业基本信息ID).Where(t => t.设备信息ID == letport2.ID).Where(t => t.监测时间 >= begin).FirstOrDefault(t => t.监测时间 < end);
                    if (master2 == null) return;
                    List<HourDataDetail> allDetails = new List<HourDataDetail>();
                    var details1 = master1.HourDataDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.HourDataDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    bool flag1 = false;
                    bool flag2 = false;
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            flag1 = true;
                            detail1 = new HourDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master1.时段量 * item.Avg / 1000.0 // item.Cou
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            flag2 = true;
                            detail2 = new HourDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master2.时段量 * item.Avg / 1000.0//item.Cou
                            };
                            details2.Add(detail2);
                        }
                    }
                    if (flag1)
                    {
                        master1.是否下发 = false;
                    }
                    if (flag2)
                    {
                        master2.是否下发 = false;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2031Hf(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport1 = CommonInformation.GetLetportByMn(mn);
                    if (letport1 == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                    if (enterprise == null) return;
                    if (!enterprise.企业名称.Contains("恒发")) return;
                    var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                     .FirstOrDefault(t => t.ID != letport1.ID);
                    if (letport2 == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, 0, 0);
                    var end = begin.AddDays(1);

                    var master1 = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport1.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);
                    if (master1 == null) return;
                    var master2 = pCtx.DayDataMains.Where(t => t.企业基本信息ID == letport2.企业基本信息ID).Where(t => t.设备信息ID == letport2.ID).Where(t => t.监测时间 >= begin).FirstOrDefault(t => t.监测时间 < end);
                    if (master2 == null) return;
                    master1.监测时间 = begin;
                    master2.监测时间 = begin;
                    List<DayDataDetail> allDetails = new List<DayDataDetail>();
                    var details1 = master1.DayDataDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.DayDataDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    bool flag1 = false;
                    bool flag2 = false;
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            flag1 = true;
                            detail1 = new DayDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master1.时段量 * item.Avg / 1000.0// item.Cou
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            flag2 = true;
                            detail2 = new DayDataDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                Avg = item.Avg,
                                Max = item.Max,
                                Min = item.Min,
                                Cou = master2.时段量 * item.Avg / 1000.0//item.Cou
                            };
                            details2.Add(detail2);
                        }
                    }
                    if (flag1)
                    {
                        master1.是否下发 = false;
                    }
                    if (flag2)
                    {
                        master2.是否下发 = false;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void ProcessRealMonitorBy2011Hf(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport1 = CommonInformation.GetLetportByMn(mn);
                if (letport1 == null) return;
                var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                if (enterprise == null) return;
                if (!enterprise.企业名称.Contains("恒发")) return;
                var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                      .FirstOrDefault(t => t.ID != letport1.ID);
                if (letport2 == null) return;
                try
                {
                    var master1 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport1.ID);
                    if (master1 == null) return;

                    var master2 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport2.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport2.ID);
                    if (master2 == null) return;
                    List<RealDataMonitorDetail> allDetails = new List<RealDataMonitorDetail>();
                    var details1 = master1.RealDataMonitorDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.RealDataMonitorDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            detail1 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                瞬时量 = item.瞬时量
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            detail2 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                瞬时量 = item.瞬时量
                            };
                            details2.Add(detail2);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2051Hf(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport1 = CommonInformation.GetLetportByMn(mn);
                if (letport1 == null) return;
                var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                if (enterprise == null) return;
                if (!enterprise.企业名称.Contains("恒发")) return;
                var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                      .FirstOrDefault(t => t.ID != letport1.ID);
                if (letport2 == null) return;
                try
                {
                    var master1 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport1.ID);
                    if (master1 == null) return;

                    var master2 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport2.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport2.ID);
                    if (master2 == null) return;
                    List<RealDataMonitorDetail> allDetails = new List<RealDataMonitorDetail>();
                    var details1 = master1.RealDataMonitorDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.RealDataMonitorDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            detail1 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                分钟排放量 = master1.分钟排放量 * item.瞬时量 / 1000.0 //item.分钟排放量
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            detail2 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                分钟排放量 = master2.分钟排放量 * item.瞬时量 / 1000.0//item.分钟排放量
                            };
                            details2.Add(detail2);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2061Hf(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport1 = CommonInformation.GetLetportByMn(mn);
                if (letport1 == null) return;
                var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                if (enterprise == null) return;
                if (!enterprise.企业名称.Contains("恒发")) return;
                var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                      .FirstOrDefault(t => t.ID != letport1.ID);
                if (letport2 == null) return;
                try
                {
                    var master1 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport1.ID);
                    if (master1 == null) return;

                    var master2 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport2.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport2.ID);
                    if (master2 == null) return;
                    List<RealDataMonitorDetail> allDetails = new List<RealDataMonitorDetail>();
                    var details1 = master1.RealDataMonitorDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.RealDataMonitorDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            detail1 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                小时排放量 = master1.小时排放量 * item.瞬时量 / 1000.0//item.小时排放量
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            detail2 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                小时排放量 = master2.小时排放量 * item.瞬时量 / 1000.0//item.小时排放量
                            };
                            details2.Add(detail2);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2031Hf(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport1 = CommonInformation.GetLetportByMn(mn);
                if (letport1 == null) return;
                var enterprise = CommonInformation.GetPollutionSourceById(letport1.企业基本信息ID);
                if (enterprise == null) return;
                if (!enterprise.企业名称.Contains("恒发")) return;
                var letport2 = pCtx.EquipmentInfors.Where(t => t.企业基本信息ID == letport1.企业基本信息ID)
                      .FirstOrDefault(t => t.ID != letport1.ID);
                if (letport2 == null) return;
                try
                {
                    var master1 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport1.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport1.ID);
                    if (master1 == null) return;

                    var master2 = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport2.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport2.ID);
                    if (master2 == null) return;
                    List<RealDataMonitorDetail> allDetails = new List<RealDataMonitorDetail>();
                    var details1 = master1.RealDataMonitorDetails;
                    foreach (var detail in details1)
                    {
                        allDetails.Add(detail);
                    }
                    var details2 = master2.RealDataMonitorDetails;
                    foreach (var detail in details2)
                    {
                        allDetails.Add(detail);
                    }
                    foreach (var item in allDetails)
                    {
                        var detail1 = details1.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail1 == null)
                        {
                            detail1 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                日排放量 = master1.日排放量 * item.瞬时量 / 1000.0// item.日排放量
                            };
                            details1.Add(detail1);
                        }
                        var detail2 = details2.FirstOrDefault(t => t.污染物编码ID == item.污染物编码ID);
                        if (detail2 == null)
                        {
                            detail2 = new RealDataMonitorDetail
                            {
                                污染物编码ID = item.污染物编码ID,
                                日排放量 = master2.日排放量 * item.瞬时量 / 1000.0//item.日排放量
                            };
                            details2.Add(detail2);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
    }
}
